package com.weetop.cfw.home_page.activity

import android.app.Activity
import android.content.Intent
import android.location.Location
import android.os.Bundle
import com.baidu.location.BDAbstractLocationListener
import com.baidu.location.BDLocation
import com.baidu.location.LocationClient
import com.baidu.location.LocationClientOption
import com.baidu.mapapi.map.*
import com.baidu.mapapi.model.LatLng
import com.blankj.utilcode.util.LogUtils
import com.weetop.cfw.R
import com.weetop.cfw.base.ui.activity.CommonBaseActivity
import kotlinx.android.synthetic.main.activity_bai_du_map.*

/**
 * 百度地图界面
 */
class BaiDuMapActivity : CommonBaseActivity(),BaiduMap.OnMapClickListener {

    private val locationListener = LocationListener()

    private lateinit var locationClient:LocationClient

    companion object{
        const val LOCATION_INFO = "LOCATION_INFO"
    }

    /**
     * 用于获取布局的ID
     */
    override fun getLayoutId() = R.layout.activity_bai_du_map

    /**
     * 初始化控件
     */
    override fun initView(savedInstanceState: Bundle?) {

    }

    /**
     * 设置数据的方法
     */
    override fun initData(savedInstanceState: Bundle?) {
        initBaiDuMap()
    }

    /**
     * 初始化百度地图
     */
    private fun initBaiDuMap(){
        val baiDuMap = baiDuMapView.map
        baiDuMap.mapType = BaiduMap.MAP_TYPE_NORMAL
        baiDuMap.isTrafficEnabled = true
        baiDuMap.isMyLocationEnabled = true
        baiDuMap.setOnMapClickListener(this)

        locationClient = LocationClient(applicationContext)
        locationClient.registerLocationListener(locationListener)
        val option = LocationClientOption()
        option.setIsNeedAddress(true)
        locationClient.locOption = option
        locationClient.start()
    }

    override fun onResume() {
        super.onResume()
        baiDuMapView.onResume()
    }

    override fun onPause() {
        super.onPause()
        baiDuMapView.onPause()
    }

    override fun onDestroy() {
        super.onDestroy()
        locationClient.unRegisterLocationListener(locationListener)
        baiDuMapView.onDestroy()
    }

    override fun onMapClick(p0: LatLng?) {
        val resultIntent = Intent()
        resultIntent.putExtra(LOCATION_INFO,p0)
        setResult(Activity.RESULT_OK,resultIntent)
        finish()
    }

    override fun onMapPoiClick(p0: MapPoi?) {

    }

    /**
     * 定位监听
     */
    private inner class LocationListener:BDAbstractLocationListener(){
        override fun onReceiveLocation(p0: BDLocation?) {
            if(p0 != null){
                val locData = MyLocationData.Builder()
                    .accuracy(10F)
                    .latitude(p0.latitude)
                    .longitude(p0.longitude).build()
                baiDuMapView.map.setMyLocationData(locData)

                val ll = LatLng(p0.latitude, p0.longitude)
                val builder = MapStatus.Builder()
                builder.target(ll).zoom(18.0f)
                baiDuMapView.map.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()))

                LogUtils.eTag("地址",p0.addrStr)
            }
        }
    }
}
